package com.glc.hms.core.compress.serializer;

import com.glc.hms.core.compress.CompressUtil;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * SNAPPY Serializer
 *
 * @author Lin
 */
public class SnappyRedisSerializer implements RedisSerializer<Object> {

    private RedisSerializer<Object> innerSerializer;

    public SnappyRedisSerializer(RedisSerializer<Object> innerSerializer) {
        this.innerSerializer = innerSerializer;
    }

    @Override
    public byte[] serialize(Object object) throws SerializationException {
        byte[] source = innerSerializer.serialize(object);
        return CompressUtil.SNAPPY.compress(source);
    }

    @Override
    public Object deserialize(byte[] target) throws SerializationException {
        byte[] decompress = CompressUtil.SNAPPY.decompress(target);
        return innerSerializer.deserialize(decompress);
    }
}